from decimal import Decimal
from .constants import Axis


# def rectIntersect(item1, item2, x, y):
#     # d1 = item1.getDimension()
#     # d2 = item2.getDimension()
#
#     d1 = item1.dimension
#     d2 = item2.dimension
#
#     cx1 = item1.position[x] + d1[x]/2
#     cx2 = item2.position[x] + d2[x] / 2
#     ix = max(cx1, cx2) - min(cx1, cx2)
#
#     cy1 = item1.position[y] + d1[y] / 2
#     cy2 = item2.position[y] + d2[y]/2
#
#     iy = max(cy1, cy2) - min(cy1, cy2)
#
#     return ix < (d1[x]+d2[x])/2 and iy < (d1[y]+d2[y])/2


def rectIntersect(item1, item2, x, y):
    # d1 = item1.getDimension()
    # d2 = item2.getDimension()

    d1 = item1.dimension
    d2 = item2.dimension

    cx1 = item1.position[x] + d1[x]/2
    cx2 = item2.position[x] + d2[x] / 2
    ix = abs(cx1 - cx2)
    if ix >= (d1[x]+d2[x])/2:
        return False

    cy1 = item1.position[y] + d1[y] / 2
    cy2 = item2.position[y] + d2[y]/2
    iy = abs(cy1 - cy2)
    if iy >= (d1[y] + d2[y]) / 2:
        return False

    return True


def intersect(item1, item2):
    return (
        rectIntersect(item1, item2, Axis.WIDTH, Axis.HEIGHT) and
        rectIntersect(item1, item2, Axis.HEIGHT, Axis.DEPTH) and
        rectIntersect(item1, item2, Axis.WIDTH, Axis.DEPTH)
    )


def getLimitNumberOfDecimals(number_of_decimals):
    return Decimal('1.{}'.format('0' * number_of_decimals))


def set2Decimal(value, number_of_decimals=0):
    number_of_decimals = getLimitNumberOfDecimals(number_of_decimals)

    return Decimal(value).quantize(number_of_decimals)

